/*
 * @Description: 
 * @Author: 韩双双
 * @Date: 2021-04-21 15:59:51
 * @LastEditTime: 2021-04-22 19:53:39
 * @LastEditors: 韩双双
 */

 /**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time: number, label: string) {
    if (time === 1) {
      return time + label
    }
    return time + label
}

/**
 * @param {number} time
 */
export function timeAgo(time: number) {
    //Date.now()1970:00:00:00 到 现在时间的毫秒数
    const between = Date.now() / 1000 - Number(time) / 1000
    if (between < 3600) {
        return pluralize(~~(between / 60), '分钟')
    } else if (between < 86400) {
        return pluralize(~~(between / 3600), '小时')
    } else if (between < 2592000) {
        return pluralize(~~(between / 86400), '天')
    } else if (between < 31536000) {
        return pluralize(~~(between / 2592000), '月')
    } else {
        return pluralize(~~(between / 31536000), '年')
    }
}

// +new Date(235625ms)表示的是需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数